<template>
  <div>
    <el-row type="flex" justify="end">
      <el-select v-model="currentYear" size="small" style="width: 120px" @change="dateChange">
        <el-option v-for="item in yearList" :key="item" :label="item" :value="item">{{ item }}</el-option>
      </el-select>
      <el-select v-model="currentMonth" size="small" style="width: 120px; margin-left: 10px" @change="dateChange">
        <el-option v-for="item in 12" :key="item" :label="item" :value="item">{{ item }}</el-option>
      </el-select>
    </el-row>
    <el-calendar v-model="currentDate">
      <!-- dateCell具名插槽，修改每个日历插件单元格 -->
      <template v-slot:dateCell="{ data, date }">
        <div class="date-content">
          <span class="text">{{ data.day | getDay }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>
<script>
export default {
  data() {
    return {
      currentDate: new Date(), // 当前的时间日期对象
      currentYear: new Date().getFullYear(), // 当前的年
      currentMonth: new Date().getMonth() + 1,
      yearList: [] // 待循环的年份
    }
  },
  created() {
    /* for (let year = this.currentYear - 5; year <= this.currentYear + 5; year++) {
      this.yearList.push(year)
    } */
    this.yearList = Array.from(Array(11), (value, index) => this.currentYear + index - 5)
  },
  methods: {
    isWeek(value) {
      return value.getDay() === 6 || value.getDay() === 0
    },
    // 改变年和月时日历插件的显示也要跟着改变
    dateChange() {
      // 循环的年也跟着重新生成
      this.yearList = Array.from(Array(11), (value, index) => this.currentYear + index - 5)
      // 默认激活每个月的1号
      this.currentDate = new Date(this.currentYear + '/' + this.currentMonth + '/1')
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep {
  .el-calendar-day {
    height: auto;
  }
  .el-calendar-table__row td,
  .el-calendar-table tr td:first-child,
  .el-calendar-table__row td.prev {
    border: none;
  }
  .el-calendar-table td.is-selected .text {
    background: #409eff;
    color: #fff;
    border-radius: 50%;
  }
  .el-calendar__header {
    display: none;
  }
}

.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text {
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
}
</style>
